/*
 * @Date: 2023-01-22 03:24:28
 * @LastEditors: 恶名扬 2080156129@qq.com
 * @LastEditTime: 2023-01-24 06:20:53
 * @FilePath: \campus-repair-platform\server\routes\reception\repair.js
 */

const express = require("express")
// 创建路由对象
const router = express.Router()

const repairService = require("../../service/reception/repair")
// 导入验证表单数据的中间件
const expressJoi = require('@escook/express-joi')
// 导入需要的验证规则对象
const { list_schema } = require("../../schema/common/repair")
const {
  add_schema,
  update_schema,
  record_add_schema,
  record_update_schema,
  return_schema, query_schema, affirm_schema
} = require("../../schema/reception/repair")

router.use((req, res, next) => {
  const type = req.auth?.type
  if (type === 1 || type === 2) {
    if (type === 1) {
      if (['/reception/return', '/reception/record'].indexOf(req.baseUrl) === -1) next()
      else return res.errSend("没有权限访问", 20002)
    }
    if (type === 2) {
      if (['/reception/add', '/reception/update'].indexOf(req.baseUrl) === -1) next()
      else return res.errSend("没有权限访问", 20002)
    }
  } else return res.errSend("没有权限访问", 10002)
})

// 获取报修单列表
router.post("/", expressJoi(list_schema), repairService.listRepair)
// 查询报修单信息
router.post("/query", expressJoi(query_schema), repairService.queryRepair)

// 提交报修申请
router.post("/add", expressJoi(add_schema), repairService.addRepairInfo)
// 修改报修单信息
router.patch("/update", expressJoi(update_schema), repairService.updateRepair)

// 学生确认完成维修/未完成维修
router.patch("/affirm", expressJoi(affirm_schema), repairService.affirmRepair)

// 维修人员接单/退单
router.patch("/return", expressJoi(return_schema), repairService.returnRepair)
// 提交维修记录
router.post(
  "/record",
  expressJoi(record_add_schema),
  repairService.addRecordInfo
)
// 根据id修改维修记录
router.patch(
  "/record",
  expressJoi(record_update_schema),
  repairService.updateRecordInfo
)

module.exports = router
